import 'dart:io';

import 'package:dio/dio.dart';
import 'package:emby/model/api_response.dart';
import 'package:emby/model/login_info.dart';
import 'package:emby/model/media.dart';
import 'package:emby/model/media_folder.dart';
import 'package:emby/model/user.dart';
import 'package:emby/util/function.dart';
import 'package:flutter/cupertino.dart';

class Api {
  static Future<ApiResponse> userPublic({String host, String port}) async {
    Response response = await Util.get("Users/Public", host: host != null ? "$host:$port/" : null);
    if (response.statusCode == 200) {
      List<User> users = response.data.map<User>((user) {
        return User.fromJson(user);
      }).toList();
      return ApiResponse(1, "获取成功", users);
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }

  static Future<ApiResponse> authenticateByName(String username, String password) async {
    Response response = await Util.post("Users/AuthenticateByName", data: {"username": username, "pw": password}, headers: {"X-Emby-Authorization": 'Emby UserId="", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"'});
    if (response.statusCode == 200) {
      return ApiResponse(1, "登录成功", LoginInfo.fromJson(response.data));
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }

  static Future<ApiResponse> userLibraryViews() async {
    var headers = {"X-Emby-Authorization": 'Emby UserId="${Util.userId}", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"', "X-Emby-Token": Util.token};
    Response response = await Util.get("Users/${Util.userId}/Views", headers: headers);
    if (response.statusCode == 200) {
      List<MediaFolder> mediaFolder = response.data['Items'].map<MediaFolder>((folder) {
        return MediaFolder.fromJson(folder);
      }).toList();
      return ApiResponse(1, "获取成功", mediaFolder);
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }

  static Future<ApiResponse> userResume({
    int limit: 20,
    String fields: "",
  }) async {
    var headers = {"X-Emby-Authorization": 'Emby UserId="${Util.userId}", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"', "X-Emby-Token": Util.token};
    Response response = await Util.get("Users/${Util.userId}/Items/Resume?Limit=$limit&Fields=$fields", headers: headers);
    if (response.statusCode == 200) {
      List<Media> medias = response.data['Items'].map<Media>((media) {
        return Media.fromJson(media);
      }).toList();
      return ApiResponse(1, "获取成功", medias);
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }

  static Future<ApiResponse> userLatestMedia({
    int limit: 20,
    String fields: "",
  }) async {
    var headers = {"X-Emby-Authorization": 'Emby UserId="${Util.userId}", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"', "X-Emby-Token": Util.token};
    Response response = await Util.get("Users/${Util.userId}/Items/latest?Limit=$limit&Fields=$fields", headers: headers);
    if (response.statusCode == 200) {
      List<Media> medias = response.data.map<Media>((media) {
        return Media.fromJson(media);
      }).toList();
      return ApiResponse(1, "获取成功", medias);
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }

  static Future<ApiResponse> mediaDetail({
    @required String id,
  }) async {
    var headers = {"X-Emby-Authorization": 'Emby UserId="${Util.userId}", Client="${Platform.isIOS ? "iOS" : "Android"}", Device="Emby Player", DeviceId="Emby Player", Version="1.0.0.0"', "X-Emby-Token": Util.token};
    Response response = await Util.get("Users/${Util.userId}/Items/$id", headers: headers);
    if (response.statusCode == 200) {
      print(response.data['DateCreated']);
      Media media = Media.fromJson(response.data);
      print(media.dateCreated);
      return ApiResponse(1, "获取成功", media);
    } else {
      return ApiResponse(response.statusCode, "", response.data);
    }
  }
}
